package com.gitee.ixtf.vertx.internal

import cn.hutool.log.Log
import com.gitee.ixtf.guice.Jguice
import graphql.schema.DataFetcher
import graphql.schema.idl.RuntimeWiring
import java.lang.reflect.Method
import kotlin.reflect.jvm.kotlinFunction

abstract class ApiMethod(val method: Method) {
  val log: Log by lazy { Log.get(instance.javaClass) }
  val instance: Any by lazy {
    method.isAccessible = true
    Jguice.getInstance(method.declaringClass)
  }
  val converters by lazy { converters(method) }
  val isSuspend by lazy { method.kotlinFunction?.isSuspend == true }
}

abstract class ApiMethodHandler(o: ApiMethod) {
  protected val log by o::log
  protected val instance by o::instance
  protected val method by o::method
  protected val converters by o::converters
  protected val kotlinFunction by lazy { method.kotlinFunction!! }
}

abstract class GraphqlMethod(method: Method) : ApiMethod(method) {
  abstract val typeName: String
  abstract val fieldName: String
  abstract val dataFetcher: DataFetcher<*>

  fun builder(builder: RuntimeWiring.Builder) {
    builder.type(typeName) { it.dataFetcher(fieldName, dataFetcher) }
  }
}
